<?php
/**
 * 登录
 * @return array|string
 */
namespace qulian\controllers;

use common\models\UserModel;
use Yii;
use yii\data\Pagination; //分页
use backend\base\BaseController;

/**
 * Site controller
 */
class LoginController extends BaseController
{
    /**
     * 登录
     * @return array
     */
    public function actionLogin()
    {
        Yii::$app->layout = 'main';
        $data = $this->body;
        if (!empty($data)) {
            $platformUser = UserModel::find()->andWhere(['status' => 1])->andWhere(['mobile' => $data['mobile']])->one();
            if ($platformUser) {
                $data['mobile'] = $platformUser->mobile;
                $model = new UserModel(['scenario' => 'login']);
                if ($model->load($data, '') && $model->validate()) {//电话登录
                    if ($admin = $model->loginValidate()) {  //查找到并且密码匹配，登录
                        // Yii::$app->user->login($admin, 3600 * 2);
                        Yii::$app->cache->set('admin', $admin->attributes, 60 * 60);
                        return json_encode(parent::ajaxReturn('operate_success_login', []));
                    }
                }
            }
            return json_encode(parent::ajaxReturn('login_error', []));//查不到账号或者密码错误
        }
        return $this->render('login');
    }

    /**
     * 登出
     * @return array
     */
    public function actionLogout()
    {
        return $this->redirect(array('site/login'));
    }

    /*
     * 默认进入首页
     */
    public function actionDemo()
    {
        Yii::$app->layout = 'main';
        return $this->render('demo', array());
    }

    /*
     * 修改密码
     */
    public function actionEditPasswordAjaxDo()
    {
        $data = parent::parseData($this->body);
        if (!empty($data)) {
            $data['id'] = Yii::$app->cache->get('admin')['id'];
            if ($data['new_password'] != $data['repet_password']) {
                return json_encode(parent::ajaxReturn('new_pass_error', []));
            }
            if ($data['new_password'] == $data['old_password']) {
                return json_encode(parent::ajaxReturn('new_old_pass_error', []));
            }
            $user = AdminModel::find()->select(['password'])->where(['id' => Yii::$app->cache->get('admin')['id']])->asArray()->one();
            if (!password_verify($data['old_password'], $user['password'])) {
                return json_encode(parent::ajaxReturn('old_pass_error', []));//原密码错误
            }
            $admin = new AdminModel(['scenario' => 'edit_pass']);
            if ($admin->load($data, '') && $admin->validate()) {
                $password = password_hash($data['new_password'], PASSWORD_DEFAULT, []);
                $count = $admin->updateAll(['password' => $password], 'id=:id', array(':id' => $data['id']));
                if ($count > 0) {
                    return json_encode(parent::ajaxReturn('operate_success_edit', []));
                }
            }
            $errorMsg = array_values($admin->getErrors());//报错信息
            return json_encode(parent::ajaxCustom('80000', $errorMsg));
        }
        return json_encode(parent::ajaxCustom('80000', '参数错误', []));//返回参数验证失败信息
    }
}